'use strict';

const { Controller } = require('egg');



class userController extends Controller {
  async login() {

    //登录
    const { ctx } = this;

    let formUn = this.ctx.request.body.user.username;
    let formAcc = this.ctx.request.body.user.account;

    //生成token
    let token = this.app.jwt.sign(formUn, this.app.config.jwt.secret);

    //按照username到数据库里检测
    let userOne = await this.app.model.User.findAll({ where: { username: formUn } });

    //如果没有找到该用户名，提醒用户不存在
    if (userOne.length === 0) {
      this.ctx.body = {
        code: 400,
        token: ''
      }
      // await this.app.model.User.create({ username: formUn, account: formAcc });
    } else {


      //密码错误
      if (userOne[0].account != formAcc) {
        this.ctx.body = {
          code: 300,
          token: ''
        }
      } else {
        this.ctx.body = {
          code: 200,
          token: token
        }
      }


    }

  }

}

module.exports = userController;
